package com.chancingpack.controller;

import com.chancingpack.bean.WxReceiptEvaluateBean;
import com.chancingpack.dal.model.WxReceiptEvaluate;
import com.chancingpack.dal.model.WxSendMessage;
import com.chancingpack.service.ReceiptEvaluateService;
import com.chancingpack.util.CommonPage;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/7/18.
 */
@Controller
@RequestMapping("receiptEvaluate")
public class ReceiptEvaluateController {

    @Autowired
    private ReceiptEvaluateService receiptEvaluateService;

    /**
     * 返回录入收货及时率数据的主页面
     * @return
     */
    @RequestMapping("/insert")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("receiptEvaluate/receiptEvaluateList");
        view.addObject("CustNameList",receiptEvaluateService.selectcustName());
        return view;
    }

    /**
     * 录入录入收货及时率信息数据
     * @param bean
     * @return
     */
    /*@RequestMapping("/saveInsert")
    public @ResponseBody
    boolean save(@RequestBody WxReceiptEvaluateBean bean) {
        return receiptEvaluateService.insertReceiptEvaluate(bean);
    }*/
    @RequestMapping(value = "/saveInsert",produces="application/json;charset=UTF-8")
    @ResponseBody
    public CommonResult saveInsert(@RequestBody WxReceiptEvaluateBean bean) {
        CommonResult result = this.receiptEvaluateService.insertReceiptEvaluate(bean);
        return result;
    }

    @RequestMapping("/saveSend")
    @ResponseBody
    public CommonResult saveSend(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = this.receiptEvaluateService.selectDetailIdByCustId(commonPage,request,response);
        if (result.getIsSuccess()) {
            result.setData(commonPage);
        }
        return result;
    }
    @RequestMapping(value = "/insertReceiptEvaluate",produces="application/json;charset=UTF-8")
    public ModelAndView insertReceiptEvaluate(String PROD_NAME,Integer detailId){
        /*String titlenew=new String(PROD_NAME.trim().getBytes("ISO-8859-1"), "UTF-8");
        req.setCharacterEncoding("utf-8");*/
        String PRODNAME=enCodeStr(PROD_NAME);
        ModelAndView view = new ModelAndView("receiptEvaluate/addReceiptEvaluate");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("PROD_NAME",PRODNAME);
        map.put("detailId",detailId);
        view.addObject("map",map);
        return view;
    }

    @RequestMapping("/selectReceiptEvaluate")
    public ModelAndView selectReceiptEvaluate(Integer detailId){
        ModelAndView view = new ModelAndView("receiptEvaluate/selectReceiptEvaluate");
        List<Map<String,Object>> wxReceiptEvaluateMap=receiptEvaluateService.selectReceiptEvaluate(detailId);
        view.addObject("wxReceiptEvaluateMap",wxReceiptEvaluateMap);
        return view;
    }
    /**
     * 解决get方式传参乱码
     * @param str 乱码字符串
     * @return 返回转码结果
     */
    public static String enCodeStr(String str) {
        try {
            return new String(str.getBytes("iso-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
